using Senparc.Weixin.Entities;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.QY.AdvancedAPIs.MailList;
using Senparc.Weixin.QY.CommonAPIs;
using System;
namespace Senparc.Weixin.QY.AdvancedAPIs
{
	public static class MailListApi
	{
		public static CreateDepartmentResult CreateDepartment(string accessToken, string name, int parentId, int order = 1, int? id = null, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={0}", accessToken);
			var data = new
			{
				name = name,
				parentid = parentId,
				order = order,
				id = id
			};
			return CommonJsonSend.Send<CreateDepartmentResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult UpdateDepartment(string accessToken, string id, string name, int parentId, int order = 1, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={0}", accessToken);
			var data = new
			{
				id = id,
				name = name,
				parentid = parentId,
				order = order
			};
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult DeleteDepartment(string accessToken, string id)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={0}&id={1}", accessToken, id);
			return Get.GetJson<QyJsonResult>(url, null);
		}
		public static GetDepartmentListResult GetDepartmentList(string accessToken, int? id = null)
		{
			string text = string.Format("https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={0}", accessToken);
			if (id.HasValue)
			{
				text += string.Format("&id={0}", id.Value);
			}
			return Get.GetJson<GetDepartmentListResult>(text, null);
		}
		public static QyJsonResult CreateMember(string accessToken, string userId, string name, int[] department = null, string position = null, string mobile = null, string email = null, string weixinId = null, string avatarMediaid = null, Extattr extattr = null, int timeOut = 10000)
		{
			string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={0}";
			var data = new
			{
				userid = userId,
				name = name,
				department = department,
				position = position,
				mobile = mobile,
				email = email,
				weixinid = weixinId,
				avatar_mediaid = avatarMediaid,
				extattr = extattr
			};
			return CommonJsonSend.Send<QyJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult UpdateMember(string accessToken, string userId, string name = null, int[] department = null, string position = null, string mobile = null, string email = null, string weixinId = null, int enable = 1, string avatarMediaid = null, Extattr extattr = null, int timeOut = 10000)
		{
			string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={0}";
			var data = new
			{
				userid = userId,
				name = name,
				department = department,
				position = position,
				mobile = mobile,
				email = email,
				weixinid = weixinId,
				enable = enable,
				avatar_mediaid = avatarMediaid,
				extattr = extattr
			};
			return CommonJsonSend.Send<QyJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult DeleteMember(string accessToken, string userId)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={0}&userid={1}", accessToken, userId);
			return Get.GetJson<QyJsonResult>(url, null);
		}
		public static QyJsonResult BatchDeleteMember(string accessToken, string[] userIds, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token={0}", accessToken);
			var data = new
			{
				useridlist = userIds
			};
			return CommonJsonSend.Send<QyJsonResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static GetMemberResult GetMember(string accessToken, string userId)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}", accessToken, userId);
			return Get.GetJson<GetMemberResult>(url, null);
		}
		public static GetDepartmentMemberResult GetDepartmentMember(string accessToken, int departmentId, int fetchChild, int status)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={0}&department_id={1}&fetch_child={2}&status={3}", new object[]
			{
				accessToken,
				departmentId,
				fetchChild,
				status
			});
			return Get.GetJson<GetDepartmentMemberResult>(url, null);
		}
		public static GetDepartmentMemberInfoResult GetDepartmentMemberInfo(string accessToken, int departmentId, int fetchChild, int status)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={0}&department_id={1}&fetch_child={2}&status={3}", new object[]
			{
				accessToken,
				departmentId,
				fetchChild,
				status
			});
			return Get.GetJson<GetDepartmentMemberInfoResult>(url, null);
		}
		public static InviteMemberResult InviteMember(string accessToken, string userId, int timeOut = 10000)
		{
			string urlFormat = string.Format("https://qyapi.weixin.qq.com/cgi-bin/invite/send?access_token={0}", accessToken);
			var data = new
			{
				userid = userId
			};
			return CommonJsonSend.Send<InviteMemberResult>(null, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static CreateTagResult CreateTag(string accessToken, string tagName, int? tagId = null, int timeOut = 10000)
		{
			string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token={0}";
			var data = new
			{
				tagname = tagName,
				tagid = tagId
			};
			return CommonJsonSend.Send<CreateTagResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult UpdateTag(string accessToken, int tagId, string tagName, int timeOut = 10000)
		{
			string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token={0}";
			var data = new
			{
				tagid = tagId,
				tagname = tagName
			};
			return CommonJsonSend.Send<QyJsonResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static QyJsonResult DeleteTag(string accessToken, int tagId)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token={0}&tagid={1}", accessToken, tagId);
			return Get.GetJson<QyJsonResult>(url, null);
		}
		public static GetTagMemberResult GetTagMember(string accessToken, int tagId)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token={0}&tagid={1}", accessToken, tagId);
			return Get.GetJson<GetTagMemberResult>(url, null);
		}
		public static AddTagMemberResult AddTagMember(string accessToken, int tagId, string[] userList = null, int[] partyList = null, int timeOut = 10000)
		{
			string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token={0}";
			var data = new
			{
				tagid = tagId,
				userlist = userList,
				partylist = partyList
			};
			return CommonJsonSend.Send<AddTagMemberResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static DelTagMemberResult DelTagMember(string accessToken, int tagId, string[] userList, int timeOut = 10000)
		{
			string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token={0}";
			var data = new
			{
				tagid = tagId,
				userlist = userList
			};
			return CommonJsonSend.Send<DelTagMemberResult>(accessToken, urlFormat, data, CommonJsonSendType.POST, timeOut);
		}
		public static GetTagListResult GetTagList(string accessToken)
		{
			string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token={0}", accessToken);
			return Get.GetJson<GetTagListResult>(url, null);
		}
	}
}
